package com.ruoyi.useradmin.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.useradmin.mapper.UaUserMapper;
import com.ruoyi.useradmin.service.IUaUserService;
import com.ruoyi.useradmin.domain.UaUser;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Value;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Map;

/**
 * @Description  
 * @Author kemonvvv
 * @Date 2023-06-07 
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class UaUserServiceImpl implements IUaUserService {


    @Resource
    private UaUserMapper uaUserMapper;
    
    

    
    
    /**
     * 全查询
     * 
     * @param pageDomain 分页信息
     * @param uaUser 要查询的实体类
     * @return 结果
     */
    @Override
    public Page<UaUser> getUaUserPage(PageDomain pageDomain, UaUser uaUser) {
        if (Objects.isNull(uaUser)) {
            return new Page<>();
        }
        try {
            LambdaQueryWrapper<UaUser> queryWrapper = new LambdaQueryWrapper<>(new UaUser());
            //if(!Objects.isNull(uaUser.getName())){
            //	queryWrapper.like(UaUser::getName,uaUser.getName());
            //}
            

            if (!Objects.isNull(uaUser.getId())) {
                queryWrapper.eq(UaUser::getId, uaUser.getId());
            }

            if (!Objects.isNull(uaUser.getOpenid())) {
                queryWrapper.like(UaUser::getOpenid, uaUser.getOpenid());
            }

            if (!Objects.isNull(uaUser.getNickname())) {
                queryWrapper.like(UaUser::getNickname, uaUser.getNickname());
            }

            if (!Objects.isNull(uaUser.getSex())) {
                queryWrapper.like(UaUser::getSex, uaUser.getSex());
            }

            if (!Objects.isNull(uaUser.getProvince())) {
                queryWrapper.like(UaUser::getProvince, uaUser.getProvince());
            }

            if (!Objects.isNull(uaUser.getCity())) {
                queryWrapper.like(UaUser::getCity, uaUser.getCity());
            }

            if (!Objects.isNull(uaUser.getConuntry())) {
                queryWrapper.like(UaUser::getConuntry, uaUser.getConuntry());
            }

            if (!Objects.isNull(uaUser.getPhotoUrl())) {
                queryWrapper.like(UaUser::getPhotoUrl, uaUser.getPhotoUrl());
            }

            if (!Objects.isNull(uaUser.getLanguage())) {
                queryWrapper.like(UaUser::getLanguage, uaUser.getLanguage());
            }

            if (!Objects.isNull(uaUser.getPurePhoneNumber())) {
                queryWrapper.like(UaUser::getPurePhoneNumber, uaUser.getPurePhoneNumber());
            }

            if (!Objects.isNull(uaUser.getStatus())) {
                queryWrapper.like(UaUser::getStatus, uaUser.getStatus());
            }

            if (!Objects.isNull(uaUser.getDelFlag())) {
                queryWrapper.like(UaUser::getDelFlag, uaUser.getDelFlag());
            }

            if (!Objects.isNull(uaUser.getCreateBy())) {
                queryWrapper.like(UaUser::getCreateBy, uaUser.getCreateBy());
            }

            if (!Objects.isNull(uaUser.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(uaUser.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(uaUser.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(UaUser::getCreateTime, uaUser.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(uaUser.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(UaUser::getCreateTime, uaUser.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(uaUser.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(uaUser.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(UaUser::getCreateTime, uaUser.getParams().get("CreateTimeBeginTime"), uaUser.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(uaUser.getUpdateBy())) {
                queryWrapper.like(UaUser::getUpdateBy, uaUser.getUpdateBy());
            }

            if (!Objects.isNull(uaUser.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(uaUser.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(uaUser.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(UaUser::getUpdateTime, uaUser.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(uaUser.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(UaUser::getUpdateTime, uaUser.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(uaUser.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(uaUser.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(UaUser::getUpdateTime, uaUser.getParams().get("UpdateTimeBeginTime"), uaUser.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(uaUser.getRemark())) {
                queryWrapper.like(UaUser::getRemark, uaUser.getRemark());
            }
        Page<UaUser> uaUserList= uaUserMapper.selectPage(pageDomain.getPage(), queryWrapper);
        for(UaUser record:uaUserList.getRecords()){
        }

            return uaUserList;
        } catch (Exception e) {
            e.printStackTrace();
            return new Page<>();
        }
    }
    

    
    /**
     * 查询通过id
     * 
     * @param id 实体类的主键
     * @return 结果
     */
    @Override
    public UaUser getUaUserById(Long id) {
        if (Objects.isNull(id)) {
            return new UaUser();
        }
        try {
            UaUser uaUser = uaUserMapper.selectById(id);
            if (Objects.isNull(uaUser)){
                uaUser = new UaUser();
            }
            return uaUser;

        } catch (Exception e) {
            e.printStackTrace();
            return new UaUser();
        }
    }


    
    
    /**
     * 通过类查询的属性值 id列表
     * 
     * @param uaUser 要查询的实体类
     * @return 结果
     */
    @Override
    public List<Long> getIdListByUaUser(UaUser uaUser) {
        if (Objects.isNull(uaUser)) {
            return new ArrayList<>();
        }
        try {
            LambdaQueryWrapper<UaUser> queryWrapper = new LambdaQueryWrapper<>(new UaUser());
            

            if (!Objects.isNull(uaUser.getId())) {
                queryWrapper.eq(UaUser::getId, uaUser.getId());
            }

            if (!Objects.isNull(uaUser.getOpenid())) {
                queryWrapper.like(UaUser::getOpenid, uaUser.getOpenid());
            }

            if (!Objects.isNull(uaUser.getNickname())) {
                queryWrapper.like(UaUser::getNickname, uaUser.getNickname());
            }

            if (!Objects.isNull(uaUser.getSex())) {
                queryWrapper.like(UaUser::getSex, uaUser.getSex());
            }

            if (!Objects.isNull(uaUser.getProvince())) {
                queryWrapper.like(UaUser::getProvince, uaUser.getProvince());
            }

            if (!Objects.isNull(uaUser.getCity())) {
                queryWrapper.like(UaUser::getCity, uaUser.getCity());
            }

            if (!Objects.isNull(uaUser.getConuntry())) {
                queryWrapper.like(UaUser::getConuntry, uaUser.getConuntry());
            }

            if (!Objects.isNull(uaUser.getPhotoUrl())) {
                queryWrapper.like(UaUser::getPhotoUrl, uaUser.getPhotoUrl());
            }

            if (!Objects.isNull(uaUser.getLanguage())) {
                queryWrapper.like(UaUser::getLanguage, uaUser.getLanguage());
            }

            if (!Objects.isNull(uaUser.getPurePhoneNumber())) {
                queryWrapper.like(UaUser::getPurePhoneNumber, uaUser.getPurePhoneNumber());
            }

            if (!Objects.isNull(uaUser.getStatus())) {
                queryWrapper.like(UaUser::getStatus, uaUser.getStatus());
            }

            if (!Objects.isNull(uaUser.getDelFlag())) {
                queryWrapper.like(UaUser::getDelFlag, uaUser.getDelFlag());
            }

            if (!Objects.isNull(uaUser.getCreateBy())) {
                queryWrapper.like(UaUser::getCreateBy, uaUser.getCreateBy());
            }

            if (!Objects.isNull(uaUser.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(uaUser.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(uaUser.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(UaUser::getCreateTime, uaUser.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(uaUser.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(UaUser::getCreateTime, uaUser.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(uaUser.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(uaUser.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(UaUser::getCreateTime, uaUser.getParams().get("CreateTimeBeginTime"), uaUser.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(uaUser.getUpdateBy())) {
                queryWrapper.like(UaUser::getUpdateBy, uaUser.getUpdateBy());
            }

            if (!Objects.isNull(uaUser.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(uaUser.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(uaUser.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(UaUser::getUpdateTime, uaUser.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(uaUser.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(UaUser::getUpdateTime, uaUser.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(uaUser.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(uaUser.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(UaUser::getUpdateTime, uaUser.getParams().get("UpdateTimeBeginTime"), uaUser.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(uaUser.getRemark())) {
                queryWrapper.like(UaUser::getRemark, uaUser.getRemark());
            }

            List<UaUser> uaUsers = uaUserMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (UaUser uaUserNo:uaUsers){
                idList.add(uaUserNo.getId());
            }
            return idList;
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

    
    /**
     * 添加
     * 
     * @param uaUser 要添加的实体类
     * @return 结果
     */
    @Override
    public int insertUaUser(UaUser uaUser) {
        if (Objects.isNull(uaUser)) {
            return -1;
        }
        try {
        
            //uaUser.setSpell(PinYin.getPinYin(uaUser.getName()));
            return uaUserMapper.insert(uaUser);
        } catch (Exception e) {
            return -1;
        }
    }

    /**
     * 更新通过id进行更新一条
     * 
     * @param uaUser 实体类
     * @return 结果
     */
    @Override
    public int updateUaUserById(UaUser uaUser) {
        if (Objects.isNull(uaUser)) {
            return -1;
        }
        try {
            //uaUser.setSpell(PinYin.getPinYin(uaUser.getName()));
            return uaUserMapper.updateById(uaUser);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 删除通过id集合
     * 
     * @param idList id集合
     * @return 结果
     */
    @Override
    public int deleteUaUserByIdList(List idList){
        if (Objects.isNull(idList)) {
            return -1;
        }
        try {
            return uaUserMapper.deleteBatchIds(idList);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 查询无条件全部 用于导出
     * 
     * @return 结果
     */
    @Override
    public List<UaUser> getUaUserList() {
        try {
            LambdaQueryWrapper<UaUser> queryWrapper = new LambdaQueryWrapper<>(new UaUser());
            queryWrapper.isNotNull(UaUser::getId);
            return uaUserMapper.selectList(queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

}
